package com.oneonefive.trainticketsystem.interceptor;

import com.oneonefive.trainticketsystem.DTO.UserDTO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        String pathInfo = request.getPathInfo();
        UserDTO user = (UserDTO) session.getAttribute("user");
        //System.out.println(user.toString());
        if (session.getAttribute("user") == null) {
            response.setStatus(401);
            log.debug("拦截到的path信息：{}", pathInfo);
            return false;
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("我获取到了一个返回的结果：" + response + " 请求结束了");
    }
}
